package com.yingjie.yingjieorder.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.yingjie.commonbase.commons.DateUtil;
import com.yingjie.commonbase.commons.ReturnObject;
import com.yingjie.commonbase.dto.PageLimitDTO;
import com.yingjie.commonbase.vo.PageLimitVO;
import com.yingjie.commonsecurity.AuthUserContext;
import com.yingjie.yingjieorder.common.OrderRabbitMQUtil;
import com.yingjie.yingjieorder.common.OrderStatus;
import com.yingjie.yingjieorder.dto.SubimtOrderDTO;
import com.yingjie.yingjieorder.model.Order;
import com.yingjie.yingjieorder.model.OrderFood;
import com.yingjie.yingjieorder.service.OrderFoodService;
import com.yingjie.yingjieorder.service.OrderService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/order")
public class OrderContorller {

    @Resource
    private OrderService orderService;
    @Resource
    private OrderFoodService orderFoodService;

    @Resource
    private OrderRabbitMQUtil orderRabbitMQUtil;
    //提交订单
    @PostMapping("/subimtOrder")
    @Transactional
    public ReturnObject subimtOrder(@RequestBody SubimtOrderDTO subimtOrderDTO) throws JsonProcessingException {
        Integer userId = AuthUserContext.get().getUserId();

        Integer orderId = orderService.subimtOrder(subimtOrderDTO,userId);
        Integer res1 = orderFoodService.subimtOrderFoods(subimtOrderDTO.getOrderFoods(),orderId,userId);
        //发动定时消息，到时间自动完成
        Date cookTime = DateUtil.TimeToDateExample(subimtOrderDTO.getCookTime());
        Long delay = cookTime.getTime() - new Date().getTime();
        if(delay<0)delay=0L;
        orderRabbitMQUtil.sendDelayOrderMsg(orderId,delay);

        return ReturnObject.SUCCESS();
    }

    //下单方的订单列表
    @PostMapping("/placeOrderList")
    public ReturnObject placeOrderList(@RequestBody PageLimitDTO pageLimitDTO){
        Integer userId = AuthUserContext.get().getUserId();
        Integer counts = orderService.placeOrderListCount(userId);
        Integer page = pageLimitDTO.getPage() - 1;
        Integer size = pageLimitDTO.getSize();
        page = page*size;
        ArrayList<Order> orders = orderService.placeOrderList(userId);
        //筛选出
        List<Order> collect =  orders.stream().filter(new Predicate<Order>() {
            @Override
            public boolean test(Order order) {
                System.out.println(order);
                //获取订单状态存在筛选订单，否所返回所有订单
                if(pageLimitDTO.getStatus().equals(OrderStatus.AWAIT_ING)||pageLimitDTO.getStatus().equals(OrderStatus.TAKE_ORDER)||pageLimitDTO.getStatus().equals(OrderStatus.FINISH)){
                    return order.getOrderStatus().equals(pageLimitDTO.getStatus());
                }else{
                    return true;
                }
            }
        }).skip(page).limit(size).collect(Collectors.toList());


        PageLimitVO pageLimitVO = new PageLimitVO();
        pageLimitVO.setList((ArrayList<Order>) collect);
        pageLimitVO.setAllCount(counts);
        return ReturnObject.SUCCESS(pageLimitVO);
    }


    //获取订单详情
    @GetMapping("/getOrderDetail/{orderId}")
    public ReturnObject getOrderDetail(@PathVariable("orderId") Integer orderId){

        Order order = orderService.getOrderDetail(orderId);
        if(order==null){
            return ReturnObject.ERROR("查询错误请检查订单id");
        }
        return ReturnObject.SUCCESS(order);
    }



    //接单方的订单列表

    @PostMapping("/acceptOrderList")
    public ReturnObject acceptOrderList(@RequestBody PageLimitDTO pageLimitDTO){
        Integer userId = AuthUserContext.get().getUserId();
        Integer counts = orderService.acceptOrderListCount(userId);
        Integer page = pageLimitDTO.getPage() - 1;
        Integer size = pageLimitDTO.getSize();
        page = page*size;
        ArrayList<Order> orders = orderService.acceptOrderList(userId);
        //筛选出
        List<Order> collect =  orders.stream().filter(new Predicate<Order>() {
            @Override
            public boolean test(Order order) {
                System.out.println(order);
                //获取订单状态存在筛选订单，否所返回所有订单
                if(pageLimitDTO.getStatus().equals(OrderStatus.AWAIT_ING)||pageLimitDTO.getStatus().equals(OrderStatus.TAKE_ORDER)||pageLimitDTO.getStatus().equals(OrderStatus.FINISH)){
                    return order.getOrderStatus().equals(pageLimitDTO.getStatus());
                }else{
                    return true;
                }
            }
        }).skip(page).limit(size).collect(Collectors.toList());


        PageLimitVO pageLimitVO = new PageLimitVO();
        pageLimitVO.setList((ArrayList<Order>) collect);
        pageLimitVO.setAllCount(counts);
        return ReturnObject.SUCCESS(pageLimitVO);
    }


    //接单
    @GetMapping("/acceptOrder/{orderId}")
    public ReturnObject acceptOrder(@PathVariable("orderId") Integer orderId){

        Integer result = orderService.acceptOrder(orderId);
        if(result.equals(0)){
            return ReturnObject.ERROR("请检查订单id");
        }
        return ReturnObject.SUCCESS(result);
    }

    //完成订单
    @GetMapping("/finishOrder/{orderId}")
    public ReturnObject finishOrder(@PathVariable("orderId") Integer orderId){

        Integer result = orderService.finishOrder(orderId);
        if(result.equals(0)){
            return ReturnObject.ERROR("请检查订单id");
        }
        return ReturnObject.SUCCESS(result);
    }

}
